package cn.com.anypay.manager.controller.auth;

import cn.com.anypay.manager.service.LoginAuthService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/auth/sms-login")
public class LoginAuthController {

    @Resource
    private LoginAuthService loginAuthService;

    @PostMapping("/send")
    public ResponseEntity<Map<String, Object>> sendSmsCode(@RequestBody Map<String, String> request) {
        try {
            boolean success = loginAuthService.senSmsCode(request);
            if (success) {
                return ResponseEntity.ok(Map.of(
                    "code", 0,
                    "message", "验证码发送成功"
                ));
            } else {
                return ResponseEntity.ok(Map.of(
                    "code", -1,
                    "message", "验证码发送失败"
                ));
            }
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                "code", -1,
                "message", "发送验证码失败: " + e.getMessage()
            ));
        }
    }

    @PostMapping("/verify")
    public ResponseEntity<Map<String, Object>> verifySmsCode(@RequestBody Map<String, String> request) {
        try {
            Map<String, Object> result = loginAuthService.verifySmsCode(request);
            if ((Boolean) result.get("success")) {
                return ResponseEntity.ok(Map.of(
                    "code", 0,
                    "message", "验证成功",
                    "data", result.get("data")
                ));
            } else {
                return ResponseEntity.ok(Map.of(
                    "code", -1,
                    "message", result.get("message")
                ));
            }
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                "code", -1,
                "message", "验证码验证失败: " + e.getMessage()
            ));
        }
    }


    
}
